package cn.cnf.controller;

import cn.cnf.common.Result;
import cn.cnf.common.enums.StateEnum;
import cn.cnf.common.enums.StatusCode;
import cn.cnf.common.exception.CustomException;
import cn.cnf.entity.ComicDetails;
import cn.cnf.service.ComicDetailsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务控制器
 *
 * @author ZhuMeiYi
 * @description
 * @since 2020-12-04 15:18:43
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/comicDetails")
public class ComicDetailsController {
    @Autowired
    private ComicDetailsService comicDetailsService;

    @GetMapping("/deleteDetails")
    public Result deleteDetails(@RequestParam("comicId") String comicId,
                                @RequestParam("comicChapterId") String comicChapterId){
        try {
        LambdaQueryWrapper<ComicDetails> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ComicDetails::getComicId,comicId)
                .eq(ComicDetails::getComicChapterId,comicChapterId);
        List<ComicDetails> list = comicDetailsService.list(wrapper);

        for(ComicDetails comicDetails : list){
            comicDetailsService.removeById(comicDetails.getId());
        }
            return Result.success();
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }

    }

}